﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;

namespace mamnontuoitho7.backend
{
    public partial class ShareLink : System.Web.UI.Page
    {

        private Link _shareLink = new Link();
        protected DataTable _dttShareLink = new DataTable();
        protected General _general = new General();

        protected void Page_Load(object sender, EventArgs e)
        {
            //Check Role Admin
            if (Session["memberID"].ToString() != "" && (ConstantClass.permissionInfo).IndexOf(Session["role"].ToString() + ",") != -1)
            {
                try
                {
                    if (!IsPostBack)
                    {
                        if (Request.Form["hidShareLinkID"] != null && Request.Form["hidShareLinkID"].ToString() != "")
                        {
                            _shareLink.DeleteShareLink(Request.Form["hidShareLinkID"].ToString());
                        }

                    }
                    _dttShareLink = _shareLink.GetAllShareLink();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }


        protected void btnShareLinkCreate_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtCompanyName.Text.Trim() == "" || txtLinkWebsite.Text.Trim() == "")
                {
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script language=JavaScript>alert('Tên website liên kết và địa chỉ website không được bỏ trống. ')</script>");
                    _dttShareLink = _shareLink.GetAllShareLink();
                    return;
                }

                string webAddress = txtLinkWebsite.Text.Trim();
                if (webAddress.IndexOf("http://") != -1)
                {
                    webAddress = webAddress.Substring(7);
                }

                _shareLink.ShareLinkCreate(txtCompanyName.Text.Trim(), webAddress);
                txtCompanyName.Text = "";
                txtLinkWebsite.Text = "";

                _dttShareLink = _shareLink.GetAllShareLink();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }


        protected void btnCancle_Click(object sender, EventArgs e)
        {
            txtCompanyName.Text = "";
            txtLinkWebsite.Text = "";
        }
    }
}